package com.supply.dl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.supply.dl.R;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.SelectDictionaryBean;
import com.supply.dl.bean.WorkNotesBean;
import com.supply.dl.bean.request.InStorageRequest;
import com.supply.dl.bean.request.SaveStockTakingRequest;
import com.supply.dl.bean.request.SearchStockRequest;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.DateUtils;
import com.supply.dl.utils.StringUtils;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;

/**
 * 新建盘点
 */
public class BuildCheckActivity extends BaseActivity {
    @BindView(R.id.edit_taking_name)
    EditText edtTakingName;
    @BindView(R.id.text_wh_name)
    TextView txtWhName;
    @BindView(R.id.text_stock_name)
    TextView txtStockName;
    @BindView(R.id.text_taking_mode)
    TextView txtTakingMode;
    @BindView(R.id.text_taking_type)
    TextView txtTakingType;
    @BindView(R.id.text_wl_code)
    TextView txtWlCode;
    @BindView(R.id.text_wl_name)
    TextView txtWlName;
    @BindView(R.id.text_save)
    TextView txtSave;
    @BindView(R.id.text_start_time)
    TextView txtStartTime;
    @BindView(R.id.text_end_time)
    TextView txtEndTime;

    private SelectDictionaryBean whBean;//盘点仓库
    private SelectDictionaryBean stockBean;//盘点库位
    private SelectDictionaryBean takingModeBean;//盘点方式
    private SelectDictionaryBean takingTypeBean;//盘点类型
    private SelectDictionaryBean wlBean;//物料
    private final static int ACTION_WH = 1;
    private final static int ACTION_STOCK = 2;
    private final static int ACTION_TAKING_MODE = 3;
    private final static int ACTION_TAKING_TYPE = 4;
    private final static int ACTION_WL = 5;

    // 开始和时间pickerview
    private TimePickerView startPicker, endPicker;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_build_check;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleShow(getResourcesString(R.string.new_built_check));
        showLeftBack();
        txtWhName.setOnClickListener(this);
        txtStockName.setOnClickListener(this);
        txtTakingMode.setOnClickListener(this);
        txtTakingType.setOnClickListener(this);
        txtWlCode.setOnClickListener(this);
        txtSave.setOnClickListener(this);
        txtStartTime.setOnClickListener(this);
        txtEndTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        Intent intent = new Intent(this, SelectBaseDataActivity.class);
        switch (v.getId()) {
            case R.id.text_save:
                save();
                break;
            case R.id.text_wh_name:
                intent.putExtra("action", AppConfig.BaseDataAction.WARE_SELECT_WAREHOUSE);
                startActivityForResult(intent, ACTION_WH);
                break;
            case R.id.text_stock_name:
                if (whBean == null) {
                    return;
                }
                intent.putExtra("action", AppConfig.BaseDataAction.WARE_SELECT_STOCK_POSITION);
                intent.putExtra("whId", whBean.getValue());
                startActivityForResult(intent, ACTION_STOCK);
                break;
            case R.id.text_taking_mode:
                intent.putExtra("action", AppConfig.BaseDataAction.STO_TAKING_MODE);
                startActivityForResult(intent, ACTION_TAKING_MODE);
                break;
            case R.id.text_taking_type:
                intent.putExtra("action", AppConfig.BaseDataAction.STO_TAKING_TYPE);
                startActivityForResult(intent, ACTION_TAKING_TYPE);
                break;
            case R.id.text_wl_code:
                Intent intent1 = new Intent(this, MaterialSelectActivity.class);
                startActivityForResult(intent1, ACTION_WL);
                break;
            case R.id.text_start_time:
                if (startPicker == null)
                    startPicker = new TimePickerView.Builder(BuildCheckActivity.this, new TimePickerView.OnTimeSelectListener() {
                        @Override
                        public void onTimeSelect(Date date, View v) {
                            txtStartTime.setText(DateUtils.getNormalTime(date, DateUtils.DATE_TIME_FORMAT));
                        }
                    }).setType(TimePickerView.Type.YEAR_MONTH_DAY_HOUR_MIN)
                            .setCancelText("取消")//取消按钮文字
                            .setSubmitText("确定")//确认按钮文字
                            .setTitleText("选择起点时间")//标题文字
                            .build();
                startPicker.show();
                break;
            case R.id.text_end_time:
                if (endPicker == null)
                    endPicker = new TimePickerView.Builder(BuildCheckActivity.this, new TimePickerView.OnTimeSelectListener() {
                        @Override
                        public void onTimeSelect(Date date, View v) {
                            txtEndTime.setText(DateUtils.getNormalTime(date, DateUtils.DATE_TIME_FORMAT));
                        }
                    }).setType(TimePickerView.Type.YEAR_MONTH_DAY_HOUR_MIN)
                            .setCancelText("取消")//取消按钮文字
                            .setSubmitText("确定")//确认按钮文字
                            .setTitleText("选择终点时间")//标题文字
                            .build();
                endPicker.show();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null || data.getSerializableExtra("selectBean") == null) {
            return;
        }
        switch (requestCode) {
            case ACTION_WH:
                whBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                txtWhName.setText(whBean.getText());
                break;
            case ACTION_STOCK:
                stockBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                txtStockName.setText(stockBean.getText());
                break;
            case ACTION_TAKING_MODE:
                takingModeBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                txtTakingMode.setText(takingModeBean.getText());
                break;
            case ACTION_TAKING_TYPE:
                takingTypeBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                txtTakingType.setText(takingTypeBean.getText());
                break;
            case ACTION_WL: //物料
                wlBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                txtWlCode.setText(wlBean.getCode());
                txtWlName.setText(wlBean.getText());
                break;
        }
    }

    /**
     * 保存盘点
     */
    private void save() {
        HttpManger httpManger = HttpManger.getInstance();
        httpManger.setContext(this);

        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener()
        {
            @Override public void onSuccess(BaseInfo<?> baseInfo)
            {
                showToast("保存盘点成功");
                finish();
            }

            @Override public void onFailure(String message)
            {
                showToast(message);
            }
        });

        SaveStockTakingRequest request = new SaveStockTakingRequest();

        request.setTakingName(edtTakingName.getText().toString().trim());
        request.setStartTime(txtStartTime.getText().toString().trim());
        request.setEndTime(txtEndTime.getText().toString().trim());

//        request.setEndTime(dateStart);
        if (whBean != null) {
            request.setWhId(whBean.getValue());
        }
        if (stockBean != null) {
            request.setStockId(stockBean.getValue());
        }
        if (takingModeBean != null) {
            request.setTakingMode(takingModeBean.getValue());
        }
        if (takingTypeBean != null) {
            request.setTakingType(takingTypeBean.getValue());
        }
        if (wlBean != null) {
            request.setSkuId(wlBean.getValue());
        }
        httpManger.saveStockTaking(request);
    }
}
